<template>
  <div class="setting-expdoc-container">
    <div style="margin: 15px 0;">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="add">新 增</el-button>
    </div>
    
    <!-- 表格 -->
    <div>
      <el-table :data="expDocList" style="width:100%" stripe>
        <el-table-column prop="express_name" label="快递公司" align="center"></el-table-column>
        <el-table-column prop="dot_name" label="网点名称" align="center"></el-table-column>
        <el-table-column prop="dot_code" label="网点编码" align="center"></el-table-column>
        <el-table-column prop="customer_name" label="客户号" align="center"></el-table-column>
        <el-table-column prop="name" label="发件人名称" align="center"></el-table-column>
        <el-table-column prop="phone" label="发件人联系方式" align="center"></el-table-column>
        <el-table-column prop="addr" label="发件人地址" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="shanchu"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disAdd" width="30%" :title="isAdd ? '新增' : '修改'" top="0">
      <add :isAdd="isAdd" :addData="exDocData" @success="disAdd = false; getExpDocList()" v-if="disAdd"/>
    </el-dialog>
  </div>
</template>

<script>
import { getExpDocList, delExpDoc, putExpDoc } from '@/api/setting'
import add from './components/add'

export default {
  name: 'setting-expdoc',
  components: {
    add
  },
  data() {
    return {
      page: 1,
      count: 1,

      expDocList: [],

      exDocData: {},
      isAdd: true,
      disAdd: false
    }
  },
  mounted() {
    this.getExpDocList();
  },
  methods: {
    /**
     * 获取电子面单列表
     */
    getExpDocList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10
      };
      getExpDocList(params).then(res => {
        if(res.status === 200){
          this.expDocList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.expDocList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 新增
     */
    add() {
      this.exDocData = {
        express_id:1,
        customer_name: '',
        customer_pwd: '',
        month_code: '',
        dot_code: '',
        dot_name: '',
        company: '',
        name: '',
        tel: '',
        phone: '',
        post_code: '',
        addr: '',
        province_code: '',
        city_code: '',
        area_code: '',
        province_name: '',
        city_name: '',
        area_name: '',
        towing_goods: '',
        status: '1',
        key: this.$store.state.app.activeApp.saa_key,
      }
      this.disAdd = true;
      this.isAdd = true;
    },
    /**
     * 编辑
     */
    eidt(data) {
      this.exDocData = {
        express_id: data.express_id,
        customer_name: data.customer_name,
        customer_pwd: data.customer_pwd,
        month_code: data.month_code,
        dot_code: data.dot_code,
        dot_name: data.dot_name,
        company: data.company,
        name: data.name,
        tel: data.tel,
        phone: data.phone,
        post_code: data.post_code,
        addr: data.addr,
        province_code: data.province_code,
        city_code: data.city_code,
        area_code: data.area_code,
        province_name: data.province_name,
        city_name: data.city_name,
        area_name: data.area_name,
        towing_goods: data.towing_goods,
        status: data.status,
        id: data.id,
        key: this.$store.state.app.activeApp.saa_key,
      }
      this.disAdd = true;
      this.isAdd = false;
    },
    /**
     * 删除
     */
    del(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id
      }
      delExpDoc(params).then(response => {
        if(response.status === 200){
          this.getExpDocList();
          this.$message.success('删除成功！');
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 修改状态
     */
    changeStatus(data) {
      const params = { key: this.$store.state.app.activeApp.saa_key, id: data.id, status: data.status };
      putExpDoc(params).then(res => {
        if(res.status === 200){
          this.$message.success('修改成功！');
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getExpDocList();
    },
  },
}
</script>

<style lang="scss" scoped>
.setting-expdoc-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
</style>